package gui;

import java.awt.Color;

import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import Methods.Bisection;
import Methods.FalsePosition;

@SuppressWarnings("serial")
public class BiFalse extends JFrame {
	private JTable table;
	private JPanel panel;
	private JScrollPane pane;
	private String[] head;
	private boolean isBi;

	public BiFalse(boolean isb) {
		isBi = isb;
		setTitle(isBi ? "Bi Section" : "False Position");
		init();
	}

	private void init() {
		setSize(320, 490);
		setLocation(0, 0);
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage("ico.PNG"));
		head = new String[] { "k", "a_k", "b_k", "f(c_k)" };

		Font f = new Font(Font.DIALOG_INPUT, Font.ITALIC, 12);
		panel = new JPanel();
		panel.setBackground(Color.white);
		panel.setLayout(null);
		add(panel);

		JLabel fx = new JLabel(("f(X)"));
		fx.setBounds(5, 5, 30, 20);
		fx.setForeground(Color.blue);
		fx.setFont(f);
		panel.add(fx);

		final JTextField ft = new JTextField();
		ft.setBounds(35, 5, 195, 20);
		ft.setForeground(Color.blue);
		ft.setFont(f);
		panel.add(ft);

		JLabel al = new JLabel(("a"));
		al.setBounds(235, 5, 10, 20);
		al.setForeground(Color.blue);
		al.setFont(f);
		panel.add(al);

		final JTextField at = new JTextField();
		at.setBounds(250, 5, 20, 20);
		at.setForeground(Color.blue);
		at.setFont(f);
		panel.add(at);

		JLabel bl = new JLabel(("b"));
		bl.setBounds(275, 5, 10, 20);
		bl.setForeground(Color.blue);
		bl.setFont(f);
		panel.add(bl);

		final JTextField bt = new JTextField();
		bt.setBounds(290, 5, 20, 20);
		bt.setForeground(Color.blue);
		bt.setFont(f);
		panel.add(bt);

		table = new JTable(new String[][] {}, head);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setForeground(Color.blue);
		table.setFont(f);
		table.setEnabled(false);
		pane = new JScrollPane(table);
		pane.setBounds(5, 30, 305, 400);
		panel.add(pane);

		JButton gen = new JButton("OK");
		gen.setBounds(5, 435, 80, 20);
		gen.setForeground(Color.blue);
		gen.setFocusable(false);
		gen.setFont(f);
		panel.add(gen);

		final JTextField xr = new JTextField();
		xr.setBounds(205, 435, 35, 20);
		xr.setForeground(Color.blue);
		xr.setFont(f);
		panel.add(xr);

		JLabel xl = new JLabel(("XRang"));
		xl.setBounds(170, 435, 40, 20);
		xl.setForeground(Color.blue);
		xl.setFont(f);
		panel.add(xl);

		final JTextField yr = new JTextField();
		yr.setBounds(275, 435, 35, 20);
		yr.setForeground(Color.blue);
		yr.setFont(f);
		panel.add(yr);

		JLabel rl = new JLabel(("YRang"));
		rl.setBounds(240, 435, 40, 20);
		rl.setForeground(Color.blue);
		rl.setFont(f);
		panel.add(rl);

		JButton plo = new JButton("Plot");
		plo.setBounds(90, 435, 80, 20);
		plo.setForeground(Color.blue);
		plo.setFocusable(false);
		plo.setFont(f);
		plo.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					String s = ft.getText();
					double x = Double.parseDouble(xr.getText());
					double y = Double.parseDouble(yr.getText());
					new Plotter(600, 600, x, y, s).setVisible(true);
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(null, ex.getMessage());
				}

			}
		});
		panel.add(plo);

		ActionListener ac = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					String s = ft.getText();
					double a = Double.parseDouble(at.getText());
					double b = Double.parseDouble(bt.getText());
					double[][] out = null;
					if (isBi) {
						Bisection bi = new Bisection(s, a, b);
						out = bi.solve();
					} else {
						FalsePosition fb = new FalsePosition(s, a, b);
						out = fb.solve();
					}
					String[][] tout = new String[out.length][4];
					for (int i = 0; i < tout.length; i++) {
						tout[i][0] = i + "";
						tout[i][1] = out[i][1] + "";
						tout[i][2] = out[i][3] + "";
						tout[i][3] = out[i][4] + "";
					}
					pane.remove(table);
					panel.remove(pane);
					table = new JTable(tout, head);
					table.setEnabled(false);
					pane = new JScrollPane(table);
					pane.setBounds(5, 30, 305, 400);
					panel.add(pane);
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(null, ex.getMessage());
				}
			}
		};
		ft.addActionListener(ac);
		at.addActionListener(ac);
		bt.addActionListener(ac);
		gen.addActionListener(ac);

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
}
